var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var that, app = getApp();
let interstitialAd = null;

Page({
    data: {
        id: 0,
        request: 1
    },
    onLoad: function(t) {
        this.data.id = t.id, that = this;
    },
    onShow: function() {
        wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#000000"
        });
        var t = {
            action: "buy",
            contr: "order",
            token: wx.getStorageSync("token"),
            id: this.data.id
        };
        _tools2.default.request(t, function(t) {
            that.setData(t.info);
        });
        if (wx.createInterstitialAd) {
            interstitialAd = wx.createInterstitialAd({
              adUnitId: 'adunit-42e141bfcfdcc315'
            })
            interstitialAd.onLoad(() => {})
            interstitialAd.onError((err) => {})
            interstitialAd.onClose(() => {})
          };
          
          // 在适合的场景显示插屏广告
          if (interstitialAd) {
            interstitialAd.show().catch((err) => {
              console.error(err)
            })
          };
    },
    gotoAddress: function() {
        wx.navigateTo({
            url: "/bh_bargain/pages/address/address"
        });
    },
    buy: function() {
        if (this.data.address) {
            this.data.request = 2;
            var t = {
                token: wx.getStorageSync("token"),
                id: this.data.id
            };
            _tools2.default.request(t, function(a) {
                3 == a.status ? wx.requestPayment({
                    timeStamp: a.info.paydata.timeStamp,
                    nonceStr: a.info.paydata.nonceStr,
                    package: a.info.paydata.package,
                    signType: a.info.paydata.signType,
                    paySign: a.info.paydata.paySign,
                    success: function(t) {
                        that.buySsuccess(a.info.luck_id);
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                }) : 2 == a.status ? wx.showModal({
                    title: "提示",
                    mask: !0,
                    content: a.info,
                    showCancel: !1
                }) : that.buySsuccess();
            }, function() {}, function() {}, "", !0, "entry/wxapp/buy");
        } else this.gotoAddress();
    },
    buySsuccess: function() {
        wx.showToast({
            title: "兑换成功"
        }), setTimeout(function() {
            wx.reLaunch({
                url: "/bh_bargain/pages/order/order"
            });
        }, 1500);
    },
    onShareAppMessage: function() {
        return {
            title: this.data.share.text,
            imageUrl: this.data.share.images,
            path: "bh_bargain/pages/index/index?parent_id=" + this.data.share.member_id
        };
    }
});